package com.ruoyi.functiontest.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.functiontest.mapper.InterchangeStatTestMapper;
import com.ruoyi.functiontest.domain.InterchangeStatTest;
import com.ruoyi.functiontest.service.IInterchangeStatTestService;

/**
 * 枢纽互通统计测试Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-09-25
 */
@Service
public class InterchangeStatTestServiceImpl implements IInterchangeStatTestService 
{
    @Autowired
    private InterchangeStatTestMapper interchangeStatTestMapper;

    /**
     * 查询枢纽互通统计测试
     * 
     * @param id 枢纽互通统计测试主键
     * @return 枢纽互通统计测试
     */
    @Override
    public InterchangeStatTest selectInterchangeStatTestById(Long id)
    {
        return interchangeStatTestMapper.selectInterchangeStatTestById(id);
    }

    /**
     * 查询枢纽互通统计测试列表
     * 
     * @param interchangeStatTest 枢纽互通统计测试
     * @return 枢纽互通统计测试
     */
    @Override
    public List<InterchangeStatTest> selectInterchangeStatTestList(InterchangeStatTest interchangeStatTest)
    {
        return interchangeStatTestMapper.selectInterchangeStatTestList(interchangeStatTest);
    }

    /**
     * 新增枢纽互通统计测试
     * 
     * @param interchangeStatTest 枢纽互通统计测试
     * @return 结果
     */
    @Override
    public int insertInterchangeStatTest(InterchangeStatTest interchangeStatTest)
    {
        return interchangeStatTestMapper.insertInterchangeStatTest(interchangeStatTest);
    }

    /**
     * 修改枢纽互通统计测试
     * 
     * @param interchangeStatTest 枢纽互通统计测试
     * @return 结果
     */
    @Override
    public int updateInterchangeStatTest(InterchangeStatTest interchangeStatTest)
    {
        return interchangeStatTestMapper.updateInterchangeStatTest(interchangeStatTest);
    }

    /**
     * 批量删除枢纽互通统计测试
     * 
     * @param ids 需要删除的枢纽互通统计测试主键
     * @return 结果
     */
    @Override
    public int deleteInterchangeStatTestByIds(Long[] ids)
    {
        return interchangeStatTestMapper.deleteInterchangeStatTestByIds(ids);
    }

    /**
     * 删除枢纽互通统计测试信息
     * 
     * @param id 枢纽互通统计测试主键
     * @return 结果
     */
    @Override
    public int deleteInterchangeStatTestById(Long id)
    {
        return interchangeStatTestMapper.deleteInterchangeStatTestById(id);
    }

    @Override
    public boolean hasChildById(Long id)
    {
        return interchangeStatTestMapper.hasChildById(id) > 0;
    }

    /**
     * 简化导入逻辑：直接按照Excel原始格式导入，通过id和parentid来确定数据
     * 
     * @param importList 导入数据列表
     * @param updateSupport 是否更新支持
     * @return 结果
     */
    @Override
    public String importInterchangeStatTest(List<InterchangeStatTest> importList, boolean updateSupport)
    {
        if (importList == null || importList.isEmpty())
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (InterchangeStatTest interchangeStatTest : importList)
        {
            try
            {
                // 通过id和parentid组合查询现有记录
                List<InterchangeStatTest> existingNodes = interchangeStatTestMapper.selectInterchangeStatTestByIdAndParentId(interchangeStatTest);
                if (existingNodes == null || existingNodes.isEmpty())
                {
                    // 不存在则插入新记录
                    interchangeStatTest.setCreateBy(SecurityUtils.getUsername());
                    interchangeStatTest.setCreateTime(DateUtils.getNowDate());
                    interchangeStatTestMapper.insertInterchangeStatTest(interchangeStatTest);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + interchangeStatTest.getRoadName() + " 导入成功");
                }
                else if (updateSupport)
                {
                    // 存在且支持更新则更新记录
                    InterchangeStatTest exist = existingNodes.get(0);
                    // 更新相关字段
                    if (interchangeStatTest.getRoadName() != null) {
                        exist.setRoadName(interchangeStatTest.getRoadName());
                    }
                    if (interchangeStatTest.getNodeName() != null) {
                        exist.setNodeName(interchangeStatTest.getNodeName());
                    }
                    if (interchangeStatTest.getMainLength() != null) {
                        exist.setMainLength(interchangeStatTest.getMainLength());
                    }
                    if (interchangeStatTest.getRampAvgLength() != null) {
                        exist.setRampAvgLength(interchangeStatTest.getRampAvgLength());
                    }
                    if (interchangeStatTest.getTollCenterLength() != null) {
                        exist.setTollCenterLength(interchangeStatTest.getTollCenterLength());
                    }
                    if (interchangeStatTest.getApproachAvgLength() != null) {
                        exist.setApproachAvgLength(interchangeStatTest.getApproachAvgLength());
                    }
                    if (interchangeStatTest.getRemark() != null) {
                        exist.setRemark(interchangeStatTest.getRemark());
                    }
                    exist.setUpdateBy(SecurityUtils.getUsername());
                    exist.setUpdateTime(DateUtils.getNowDate());
                    interchangeStatTestMapper.updateInterchangeStatTest(exist);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、数据 " + interchangeStatTest.getRoadName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、数据 " + interchangeStatTest.getRoadName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、数据 " + interchangeStatTest.getRoadName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
//                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    /**
     * 批量更新实测单位审核状态
     * 
     * @return 更新记录数
     */
    @Override
    public int updateReview1ToZero()
    {
        return interchangeStatTestMapper.updateReview1ToZero();
    }

    /**
     * 校验所有数据的review1字段是否都为0
     * 
     * @return 是否全部为0
     */
    @Override
    public boolean checkAllReview1IsZero()
    {
        return interchangeStatTestMapper.checkAllReview1IsZero() == 0;
    }

    /**
     * 根据条件批量更新业主审核状态
     * 
     * @param queryCondition 查询条件
     * @return 更新记录数
     */
    @Override
    public int updateReview2ToZeroByCondition(InterchangeStatTest queryCondition)
    {
        return interchangeStatTestMapper.updateReview2ToZeroByCondition(queryCondition);
    }

    /**
     * 校验所有数据的review1和review2字段是否都不为空且都为0
     * 
     * @return 是否全部为0
     */
    @Override
    public boolean checkAllReview1AndReview2AreZero()
    {
        return interchangeStatTestMapper.checkAllReview1AndReview2AreZero() == 0;
    }

    /**
     * 批量更新中心审核状态
     * 
     * @return 更新记录数
     */
    @Override
    public int updateReview3ToZero()
    {
        return interchangeStatTestMapper.updateReview3ToZero();
    }
}
